/*
 * Package: org.grassfield.common.dao
 * FileName: UserRoleDAOTest.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package dao;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import bean.MenuItem;
import bean.UserRole;

import test.BaseTest;



/**
 * A junit test case for UserRoleDAO.
 */
public class UserRoleDAOTest extends BaseTest {
	private IUserRoleDAO userRoleDAO;
	private IMenuDAO menuDAO;

	/**
	 * Sets the user role dao.
	 * 
	 * @param userRoleDAO the new user role dao
	 */
	public void setUserRoleDAO(IUserRoleDAO userRoleDAO) {
		this.userRoleDAO = userRoleDAO;
	}

	/**
	 * Sets the menu dao.
	 * 
	 * @param menuDAO the new menu dao
	 */
	public void setMenuDAO(IMenuDAO menuDAO) {
		this.menuDAO = menuDAO;
	}

	/**
	 * Test case for add.
	 */
	public void testAdd() {
		UserRole userRole = new UserRole();
		userRole.setName("test admin");
		List<MenuItem> list = this.menuDAO.list();
		Set<MenuItem> menuList = new TreeSet<MenuItem>();
		menuList.addAll(list);
		userRole.setMenuList(menuList);
		userRole = this.userRoleDAO.add(userRole);
		super.assertNotNull("id is null", userRole.getId());
		System.out.println("user role id:"+userRole.getId());
	}

	/**
	 * Test case for find.
	 */
	public void testFind() {
		long id = 1L;
		UserRole userRole = this.userRoleDAO.find(id);
		super.assertNotNull("role is null", userRole);
		super.assertTrue("ids are different", id==userRole.getId().longValue());
		System.out.println("user role id:"+userRole.getId()+" "+userRole.getName());
	}

	/**
	 * Test case for list.
	 */
	public void testList() {
		List<UserRole> list = this.userRoleDAO.list();
		super.assertNotNull("list is null", list);
		super.assertFalse("list is empty", list.isEmpty());
		for (UserRole userRole:list){
			System.out.println("user role :"+userRole.getId()+" "+userRole.getName());
		}
	}
	
	/**
	 * Test case for update.
	 */
	public void testUpdate(){
		UserRole find = this.userRoleDAO.find(1L);
		Set<MenuItem> set = new TreeSet<MenuItem>();
		set.add(this.menuDAO.find(1L));
		set.add(this.menuDAO.find(2L));
		find.setMenuList(set);
		find.setName("Modified name");
		this.userRoleDAO.update(find);
	}

}
